<template>
	<div class="system-menu-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="menuDialogFormRef" :model="state.ruleForm" size="default" label-width="80px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="上级菜单">
							<el-cascader
								:options="state.menuData"
								:props="{ checkStrictly: true, value: 'id', label: 'title' }"
								placeholder="请选择上级菜单"
								clearable
								class="w100"
								v-model="state.ruleForm.parent_id"
							>
								<template #default="{ node, data }">
									<span>{{ data.title }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="菜单类型">
							<el-radio-group v-model="state.ruleForm.menuType">
								<el-radio label="menu">菜单</el-radio>
								<el-radio label="btn">按钮</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="是否固定">
							<el-radio-group v-model="state.ruleForm.is_affix">
								<el-radio  value="1">是</el-radio>
								<el-radio value="0">否</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="菜单名称">
							<el-input v-model="state.ruleForm.title" placeholder="格式：message.router.xxx" clearable></el-input>
						</el-form-item>
					</el-col>
					<template v-if="state.ruleForm.menuType === 'menu'">
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="路由路径">
								<el-input v-model="state.ruleForm.path" placeholder="路由中的 path 值" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="菜单图标">
								<IconSelector placeholder="请输入菜单图标" v-model="state.ruleForm.icon" />
							</el-form-item>
						</el-col>
					</template>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="菜单排序">
							<el-input-number v-model="state.ruleForm.menuSort" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<template v-if="state.ruleForm.menuType === 'menu'">
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="是否隐藏">
								<el-radio-group v-model="state.ruleForm.show">
									<el-radio :label="0">隐藏</el-radio>
									<el-radio :label="1">不隐藏</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
					</template>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemMenuDialog">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { useMenuApi} from '/@/api/menu';
import { ElMessage } from 'element-plus';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 引入组件
const IconSelector = defineAsyncComponent(() => import('/@/components/iconSelector/index.vue'));

// 定义变量内容
const menuDialogFormRef = ref();
const state = reactive({
	// 参数请参考 `/src/router/route.ts` 中的 `dynamicRoutes` 路由菜单格式
	ruleForm: {
		parent_id: [], // 上级菜单
		title:'',
		menuType: 'menu', // 菜单类型
		path: '', // 路由路径
		menuSort: 0, // 菜单排序
		show:1, // 是否显示
		icon:'', // 菜单图标
		id:'',
		type:'',
		is_affix:"0"
	},
	menuData: [], // 上级菜单数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 获取 pinia 中的路由
const getMenuData = async() => {
	try{
		const response = await useMenuApi().getAdminMenu();
		state.menuData = response.data;
	}catch(error){
		// console.error('获取数据失败:', error);
	}
};
// 打开弹窗
const openDialog = (type: string, row?: any) => {
	getMenuData();
	if (type === 'edit') {
		// 模拟数据，实际请走接口
		row.menuType = 'menu';
		state.ruleForm.parent_id = row.parent_id;
		state.ruleForm.title = row.title;
		state.ruleForm.icon = row.icon;
		state.ruleForm.path = row.uri;
		state.ruleForm.id   = row.id;
		state.ruleForm.menuSort = row.order;
		state.ruleForm.type = type;
		state.dialog.title = '修改菜单';
		state.dialog.submitTxt = '修 改';
	} else if(type === 'add_menu') {
		state.dialog.title = '新增菜单';
		state.dialog.submitTxt = '新 增';
		state.ruleForm.parent_id = row.id;
		state.ruleForm.type = type;
	}else{
		state.dialog.title = '新增菜单';
		state.dialog.submitTxt = '新 增';
		state.ruleForm.type = type;
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = async () => {
	//将数据传递到后端
	if(state.ruleForm.type==='edit'){
		await useMenuApi().postEditMenu(state.ruleForm);
	}else{
		await useMenuApi().postAddMenu(state.ruleForm);
	}
	closeDialog(); // 关闭弹窗
	ElMessage.success('操作成功');
	emit('refresh');
	// if (state.dialog.type === 'add') { }
	// setBackEndControlRefreshRoutes() // 刷新菜单，未进行后端接口测试
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
